#region License
/* Copyright (c) 2011 Maksymilian Mulawa
	
Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice, 
 * this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice, 
 * this list of conditions and the following disclaimer in the documentation 
 * and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products 
 * derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System.ComponentModel;
using System.IO;
using System.Windows.Controls.WpfPropertyGrid;
using ReplicationExplorer.DomainModel.Distributor.Entities;
using ReplicationExplorer.DomainModel.Infrastructure;
using ReplicationExplorer.DomainModel.Publisher.Models;
using StringConverter = ReplicationExplorer.DomainModel.UI.StringConverter;

namespace ReplicationExplorer.DomainModel.Distributor.Models
{
    [CategoryOrder(DATABASE_CATEGORY, 0)] 
    [CategoryOrder(DATABASE_METADATA_CATEGORY, 1)] 
    [CategoryOrder(DISTRIBUTION_CATEGORY, 2)]
    [CategoryOrder(DISTRIBUTION_CLEANUP_CATEGORY, 4)]
    [CategoryOrder(DISTRIBUTION_DATABASE_FILES_CATEGORY, 4)]
    public class DistributionDatabaseViewModel : TreeViewItemViewModel
    {
        private const string DATABASE_CATEGORY = "Database";
        private const string DATABASE_METADATA_CATEGORY = "Database metadata";
        private const string DISTRIBUTION_CATEGORY = "Distribution";
        private const string DISTRIBUTION_CLEANUP_CATEGORY = "Distribution cleanup";
        private const string DISTRIBUTION_DATABASE_FILES_CATEGORY = "Distribution database files";
        

        private readonly DistributionDatabase _database;

        public DistributionDatabaseViewModel(DistributionDatabase database, DistributionDatabaseFolderViewModel parent, bool lazyLoadChildren):base(parent, lazyLoadChildren)
        {
            _database = database;
            Children.Add(new PublisherServerFolderViewModel(this, true));
        }

        public override string ModelViewIcon
        {
            get
            {
                return "DistributionDatabase.png";
            }
        }

        public override string ModelViewName
        {
            get
            {
                return "Distribution database";
            }
        }

        public override bool ShowPropertySearch
        {
            get
            {
                return true;
            }
        }

        [Category(DATABASE_CATEGORY)]
        [Description("Distribution database name.")]
        [DisplayName(@"Database name")]
        [ReadOnly(true)]
        [PropertyOrder(0)]
        public string Name { get { return _database.Name; } }

        [Category(DATABASE_METADATA_CATEGORY)]
        [Description("Distribution database state")]
        [DisplayName(@"State")]
        [ReadOnly(true)]
        [PropertyOrder(1)]
        public string DatabaseState { get { return _database.DatabaseState; } }

        [Category(DATABASE_METADATA_CATEGORY)]
        [Description("Distribution database collation.")]
        [DisplayName(@"Collation")]
        [ReadOnly(true)]
        [PropertyOrder(2)]
        public string Collation { get { return _database.Collation; } }


        [Category(DATABASE_METADATA_CATEGORY)]
        [Description("Distribution database recovery model.")]
        [DisplayName(@"Recovery model")]
        [ReadOnly(true)]
        [PropertyOrder(3)]
        public string RecoveryModel { get { return _database.RecoveryModel.ToUpperInvariant(); } }


        [Category(DATABASE_METADATA_CATEGORY)]
        [Description("Displays information if 'Sync With Backup' option is set for Distribution database.")]
        [DisplayName(@"Is 'Sync With Backup' set")]
        [ReadOnly(true)]
        [PropertyOrder(4)]
        public string IsSyncWithBackup { get { return StringConverter.ConvertBoolToYesNo(_database.IsSyncWithBackup); } }

        [Category(DISTRIBUTION_CATEGORY)]
        [Description("Minimum retension period")]
        [DisplayName(@"Minimum retension period")]
        [ReadOnly(true)]
        [PropertyOrder(5)]
        public string MinimumRetensionPeriod { get { return StringConverter.ConvertToHoursOrDays(_database.MinimumRetensionPeriod); } }

        [Category(DISTRIBUTION_CATEGORY)]
        [Description("Maximum retension period")]
        [DisplayName(@"Maximum retension period")]
        [ReadOnly(true)]
        [PropertyOrder(6)]
        public string MaximumRetensionPeriod { get { return StringConverter.ConvertToHoursOrDays(_database.MaximumRetensionPeriod); } }

        [Category(DISTRIBUTION_CATEGORY)]
        [Description("History retension period")]
        [DisplayName(@"History retension period")]
        [ReadOnly(true)]
        [PropertyOrder(7)]
        public string HistoryRetensionPeriod { get { return StringConverter.ConvertToHoursOrDays(_database.HistoryRetensionPeriod); } }

        [Category(DISTRIBUTION_CLEANUP_CATEGORY)]
        [Description("History Cleanup Sql Server Agent Job Job Name")]
        [DisplayName(@"History Cleanup Job Name")]
        [ReadOnly(true)]
        [PropertyOrder(8)]
        public string HistoryCleanupAgent { get { return _database.HistoryCleanupAgent.Name; } }

        //[Category(DISTRIBUTION_CLEANUP_CATEGORY)]
        //[Description("History Cleanup Job Next Run At")]
        //[DisplayName(@"History Cleanup Job Next Run At")]
        //[ReadOnly(true)]
        //[PropertyOrder(9)]
        //public string HistoryCleanupAgentNextRunAt { get { return "N/A"; } }

        [Category(DISTRIBUTION_CLEANUP_CATEGORY)]
        [Description("Distribution Cleanup Sql Server Agent Job Name")]
        [DisplayName(@"Distribution Cleanup Job Name")]
        [ReadOnly(true)]
        [PropertyOrder(10)]
        public string DistributionCleanupAgent { get { return _database.DistributionCleanupAgent.Name; } }

        //[Category(DISTRIBUTION_CLEANUP_CATEGORY)]
        //[Description("Distribution Cleanup Job Next Run At")]
        //[DisplayName(@"Distribution Cleanup Job Next Run At")]
        //[ReadOnly(true)]
        //[PropertyOrder(11)]
        //public string DistributionCleanupAgentNextRunAt { get { return "N/A"; } }

        [Category(DISTRIBUTION_DATABASE_FILES_CATEGORY)]
        [Description("Data File")]
        [DisplayName(@"Data File")]
        [ReadOnly(true)]
        [PropertyOrder(12)]
        public string DataFile { get { return Path.Combine(_database.DataFolder, _database.DataFile); } }

        [Category(DISTRIBUTION_DATABASE_FILES_CATEGORY)]
        [Description("Data File Size in GB")]
        [DisplayName(@"Data File Size in GB")]
        [ReadOnly(true)]
        [PropertyOrder(13)]
        public string DataFileSizeInGB { get { return StringConverter.ConvertMBToGB(_database.DataFileSizeInMB); } }

        [Category(DISTRIBUTION_DATABASE_FILES_CATEGORY)]
        [Description("Transaction Log")]
        [DisplayName(@"Transaction Log ")]
        [ReadOnly(true)]
        [PropertyOrder(14)]
        public string TransactionLogFile { get { return Path.Combine(_database.TransactionLogFolder, _database.TransactionLogFile); } }

        [Category(DISTRIBUTION_DATABASE_FILES_CATEGORY)]
        [Description("Transaction Log Size in GB")]
        [DisplayName(@"Transaction Log Size in GB")]
        [ReadOnly(true)]
        [PropertyOrder(15)]
        public string TransactionLogFileSizeInGB { get { return StringConverter.ConvertMBToGB(_database.TransactionLogFileSizeInMB); } }

        [Browsable(false)]
        public DistributionDatabase Source
        {
            get {
                return _database;
            }
        }
    }
}